<script>
import outMain from "@/components/outMain.vue";
import { ElMessage, ElMessageBox, ElLoading } from "element-plus";
import {
  interviewRecord,
  checkInterviewStatus,
  bookCheckStatus,
  interviewRecordDel,
} from "@/http/api";

export default {
  components: { outMain },
  name: "",
  data() {
    return {
      tableData: [],
      switching: false,
      statusArr: [
        { label: "全部", value: "" },
        { label: "面试中", value: "1" },
        { label: "面试成功", value: "2" },
        { label: "面试失败", value: "3" },
      ],
      signArr: [
        { label: "全部", value: "" },
        { label: "未签约", value: "1" },
        { label: "已签约", value: "2" },
      ],
      queryData: {
        b_real_name: "",
        is_sign: "",
        interview_status: "",
        page: 1,
        per_page: 20,
      },
      total: 0,
    };
  },
  props: {},
  setup() {},
  methods: {
    deleteFn(row) {
      ElMessageBox.confirm("确定删除选择的面试记录吗?", "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const { data } = await interviewRecordDel({
          invite_interview_id: row.invite_interview_id,
        });
        if (data.code == 1) {
          ElMessage({
            type: "success",
            message: "删除成功",
          });
          this.getData();
        }
      });
    },
    questCheck(data, type) {
      checkInterviewStatus(data).then((res) => {
        if (res.data.code == 1) {
          ElMessage({
            type: "success",
            message: "操作成功",
          });
          setTimeout(() => {
            this.$router.push({
              path: "/bootSignJoin",
              query: { id: data.invite_interview_id },
            });
          }, 1000);
          // this.getData();
        }
      });
    },
    checkStatus(row, type) {
      if (type == 2) {
        ElMessageBox.confirm(
          "确定将此面试结果状态更改为面试成功?",
          "操作提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        ).then(() => {
          let data = {
            invite_interview_id: row.invite_interview_id,
            interview_status: type,
          };
          this.questCheck(data, type);
        });
      } else {
        ElMessageBox.prompt(
          "确定将此面试结果状态更改为面试失败?请输入面试失败备注",
          "操作提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        ).then((value) => {
          let data = {
            invite_interview_id: row.invite_interview_id,
            interview_status: type,
            failure_reason: value.value,
          };
          this.questCheck(data, type);
        });
      }
    },
    detailFn(row) {
      this.$router.push({
        path: "/bootInviteDetail",
        query: { id: row.invite_interview_id, page: this.queryData.page },
      });
    },
    signFn(row) {
      this.$router.push({
        path: "/bootSignJoin",
        query: { id: row.invite_interview_id, page: this.queryData.page },
      });
    },
    bookChange(type, status, item) {
      let data = {
        type: type,
        status: status ? 1 : 2,
        invite_interview_id: item.invite_interview_id,
      };
      this.switching = true;
      const loading = ElLoading.service({
        lock: true,
        text: "Loading",
        background: "rgba(255, 255, 255, 0.7)",
      });
      bookCheckStatus(data)
        .then((res) => {
          if (res.data.code == 1) {
            ElMessage({
              type: "success",
              message: "操作成功",
            });

            // this.getData();
          }
          this.switching = false;
          loading.close();
        })
        .catch((err) => {
          this.switching = false;
          loading.close();
        });
    },
    async getData() {
      const { data } = await interviewRecord(this.queryData);
      if (data.code == 1) {
        let arr = data.data.data;
        for (let item of arr) {
          item["disSwitch"] = true;
          if (item.interview_status == 2 && item.is_sign == 0 && item.is_zc) {
            item["disSwitch"] = false;
          }
          item["qy_tjzt_"] = item.qy_tjzt == 1 ? true : false;
          item["qy_zsyjzt_"] = item.qy_zsyjzt == 1 ? true : false;
        }
        this.tableData = data.data.data;
        this.total = data.data.total;
      }
    },
    getSwitchText(type, status, row) {
      if (row.interview_status != 2) {
        return "请先更改面试状态";
      } else if (!row.is_zc) {
        return "需暂存签约之后才可";
      } else if (row.is_sign == 2) {
        return "已签约，不可更改状态";
      } else {
        if (status) {
          return type == 1 ? "已邮寄" : "已体检";
        } else {
          return type == 1 ? "未邮寄" : "未体检";
        }
      }
    },
  },
  mounted() {},
  created() {
    this.queryData.page = Number(localStorage.getItem("boot_page")) || 1;

    this.getData();
  },
};
</script>
<template>
  <outMain>
    <div class="tableCont">
      <div class="tableName flexCont">
        <div>船员面试记录（{{ total }}）</div>
      </div>
      <div class="tableSerch">
        <div class="serchLeft"></div>
        <div class="serchRight">
          <el-form class="flexCenter" :inline="true">
            <el-form-item>
              <el-input
                clearable
                @clear="getData"
                @keyup.enter="getData"
                v-model="queryData.b_real_name"
                placeholder="请输入船员姓名"
              />
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="queryData.is_sign"
                clearable
                @change="getData"
                placeholder="请选择签约状态"
              >
                <el-option
                  v-for="item in signArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="queryData.interview_status"
                clearable
                @change="getData"
                placeholder="请选择面试状态"
              >
                <el-option
                  v-for="item in statusArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <btn value="搜索" @click="getData"></btn>
          </el-form>
        </div>
      </div>
      <el-table class="tableMain" :data="tableData" stripe style="width: 100%">
        <el-table-column
          prop="b_real_name"
          show-overflow-tooltip
          label="船员姓名"
        >
          <template #default="{ row }">
            {{ row.b_real_name }}
          </template>
        </el-table-column>
        <el-table-column
          prop="position_name"
          show-overflow-tooltip
          label="面试岗位"
        >
          <template #default="{ row }">
            {{ row.position_name }}
          </template>
        </el-table-column>
        <el-table-column
          width="140"
          prop="interview_time"
          show-overflow-tooltip
          label="面试时间"
        />
        <el-table-column
          width="180"
          prop="cdxm"
          show-overflow-tooltip
          label="面试公司"
        >
          <template #default="{ row }">
            {{ row.cdxm }}
          </template>
        </el-table-column>
        <el-table-column label="邮寄证书">
          <template #default="{ row }">
            <div class="tagGroup">
              <el-tooltip
                :content="getSwitchText(1, row.qy_zsyjzt_, row)"
                placement="top"
              >
                <el-switch
                  @change="bookChange(1, row.qy_zsyjzt_, row)"
                  :disabled="row.disSwitch || switching"
                  v-model="row.qy_zsyjzt_"
                  style="
                    --el-switch-on-color: #13ce66;
                    --el-switch-off-color: #ff4949;
                  "
                />
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="体检状态">
          <template #default="{ row }">
            <div class="tagGroup">
              <el-tooltip
                :content="getSwitchText(2, row.qy_tjzt_, row)"
                placement="top"
              >
                <el-switch
                  @change="bookChange(2, row.qy_tjzt_, row)"
                  :disabled="row.disSwitch || switching"
                  v-model="row.qy_tjzt_"
                  style="
                    --el-switch-on-color: #13ce66;
                    --el-switch-off-color: #ff4949;
                  "
                />
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="面试状态">
          <template #default="{ row }">
            <div class="tagGroup">
              <el-tag v-if="row.interview_status == 1">面试中</el-tag>
              <el-tag type="success" v-if="row.interview_status == 2"
                >面试成功</el-tag
              >
              <el-tag type="danger" v-if="row.interview_status == 3"
                >面试失败</el-tag
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip label="签约状态">
          <template #default="{ row }">
            <div class="tagGroup">
              <el-tag type="info" v-if="row.is_sign == 0">未签约</el-tag>
              <el-tag type="success" v-if="row.is_sign == 2">已签约</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="create_time"
          width="180"
          show-overflow-tooltip
          label="创建时间"
        />
        <el-table-column
          prop="staff_name"
          show-overflow-tooltip
          label="操作员工"
        >
          <template #default="{ row }">
            {{ row.staff_name }}
          </template>
        </el-table-column>
        <el-table-column prop="Name" width="170" label="操作">
          <template #default="{ row }">
            <div class="tableCtrl">
              <div class="greenText ctrlText" @click="detailFn(row)">详情</div>
              <div
                class="blueText ctrlText"
                @click="checkStatus(row, 2)"
                v-if="row.interview_status == 1"
              >
                成功
              </div>
              <div
                class="redText ctrlText"
                @click="checkStatus(row, 3)"
                v-if="row.interview_status == 1"
              >
                失败
              </div>
              <div
                class="blueText ctrlText"
                v-if="
                  (row.is_sign == 0 || row.is_sign == 3) &&
                  row.interview_status == 2
                "
                @click="signFn(row)"
              >
                签约
              </div>
              <div class="redText ctrlText" @click="deleteFn(row)">删除</div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageCont">
        <el-pagination
          v-model:currentPage="queryData.page"
          v-model:page-size="queryData.per_page"
          background
          layout="total, prev, pager, next, jumper"
          :total="total"
          @size-change="getData"
          @current-change="getData"
        />
      </div>
    </div>
  </outMain>
</template>
<style scoped lang="less"></style>
